package com.jpx.jxcupms.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.jxc.common.vo.ResultEntity;
import com.jpx.jxcupms.entity.TUserRole;
import com.jpx.jxcupms.service.ITUserRoleService;
import com.jpx.jxcupms.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/upms/tUserRole")
public class TUserRoleController {
    @Autowired
    ITUserRoleService itUserRoleService;

    @RequestMapping("updateRole")
    @Transactional
    public ResultEntity updateRole(Integer userid,Integer[] roleids){
        Wrapper<TUserRole> wrapper = new EntityWrapper<TUserRole>();
        wrapper.eq("user_id",userid);
        itUserRoleService.delete(wrapper);
        List<TUserRole> list = new ArrayList<TUserRole>();
        for (Integer roleid:roleids) {
            TUserRole tUserRole = new TUserRole();
            tUserRole.setUserId(userid);
            tUserRole.setRoleId(roleid);
            list.add(tUserRole);
        }
        itUserRoleService.insertBatch(list);
        return ResultEntity.ok();
    }
}
